package com.cby.user.test3D;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/*
**  Create by: 王孙康宏
    Date:2021-10-24
    Time:22:46
*/
@Data
public class DataUtil {
    // 禁忌表
    public int[][] tabu;
    //
    float scaler = 1.0f;
    public DataUtil(float scaler){
        this.scaler = scaler;
    }
    /**
     * 根据文件读取单规格数据
     * @return
     */
    public Instance read(String path) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
        Instance instance = new Instance();
        String input = null;
        int counter = 0;
        List<Box> boxList = new ArrayList<>();
        while ((input=bufferedReader.readLine())!=null){
            String[] split = input.split(" ");
            if(counter==0){
                instance.setL(Float.parseFloat(split[0]));
                instance.setW(Float.parseFloat(split[1]));
                instance.setH(Float.parseFloat(split[2]));
                instance.setSupportRate(Float.parseFloat(split[3]));
                instance.setDep(Integer.parseInt(split[4]));
                counter++;
            }else{
                float l = Float.parseFloat(split[0]);
                float w = Float.parseFloat(split[1]);
                float h = Float.parseFloat(split[2]);
                int dl = Integer.parseInt(split[3]);
                int dw = Integer.parseInt(split[4]);
                int dh = Integer.parseInt(split[5]);
                int count = Integer.parseInt(split[6]);
                boxList.add(new Box(l,w,h,new int[]{dl,dw,dh},count));
            }
        }
        Box[] boxes = new Box[boxList.size()];
        for (int i = 0; i < boxList.size(); i++) {
            Box box = boxList.get(i);
            boxes[i] = new Box(box);
        }
        Arrays.sort(boxes);
        instance.setBoxes(boxes.clone());
        if(bufferedReader!=null){
            bufferedReader.close();
        }
        return instance;
    }

    /**
     * 生成随机测试数据
     */
    @Test
    public void randomGenerateData(){
        for (int i = 0; i < 40; i++) {
            int l = new Random().nextInt(70)+70;
            int w = new Random().nextInt(70)+70;
            int h = new Random().nextInt(70)+70;
            int count = new Random().nextInt(20)+20;
            System.out.printf("%d %d %d %d %d %d %d\n",l,w,h,1,1,1,count);
        }
    }
}
